package linuxbox.filelib.servlet;

import java.sql.Timestamp;
import javax.servlet.http.*;
import org.apache.log4j.Logger;

import linuxbox.BoxStatic;
import linuxbox.filelib.logic.*;
import linuxbox.common.*;

/**
 * <p>Title: Linuxbox</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author Xia,Xunfan
 * @version 1.0
 */

public class EditFileAction extends Action
{
   protected static String fileViewJSP = null;
   protected static String fileEditJSP = null;
   protected static String folderContentJSP = null;

   protected static FileLibHome home = FileLibHome.getInstance();
   protected static Logger logger = Logger.getLogger(EditFileAction.class.getName());

   public void doAction(HttpSession session, BoxSession aBoxSession,
   HttpServletRequest req, HttpServletResponse resp) throws Exception
   {
logger.debug("=========== Start Edit File =============");
     String str = req.getParameter("SubmitView");
     FileLibEntry entry = null;
     if ( str != null )
     {
       String tmp = (String) req.getParameter("ID");
       int ID = -1;
       if (tmp != null && tmp.length() > 0)
         ID = Integer.parseInt(tmp);
       if (ID != home.ROOT)
       {
         entry = home.get(ID);
         if (entry == null || entry.getIsfolder())
         {
           logger.debug("Error geting folder:" + tmp);
           sendFile(BoxStatic.errorJSP,req,resp);
           return;
         }

         if (!home.checkAccessRights(aBoxSession.getID(), entry, home.BROWSE_ALLOW))
         {
           logger.warn("Access rights violation for user " + aBoxSession.getLoginID() + " " + entry.getName());
           sendFile(BoxStatic.accessRightErrorJSP,req,resp);
           return;
         }
       }
       session.setAttribute("filelib.CURENTRY", entry);
       sendFile(fileViewJSP,req,resp);
       return;
     }

     str = req.getParameter("SubmitEdit");
     if ( str != null )
     {
       String tmp = (String) req.getParameter("ID");
       int ID = -1;
       if (tmp != null && tmp.length() > 0)
         ID = Integer.parseInt(tmp);
       if (ID != home.ROOT)
       {
         entry = home.get(ID);
         if (entry == null || entry.getIsfolder())
         {
           logger.debug("Error geting folder:" + tmp);
           sendFile(BoxStatic.errorJSP,req,resp);
           return;
         }

         if (!home.checkAccessRights(aBoxSession.getID(), entry, home.WRITE_ALLOW))
         {
           logger.warn("Access rights violation for user " + aBoxSession.getLoginID() + " " + entry.getName());
           sendFile(BoxStatic.accessRightErrorJSP,req,resp);
           return;
         }
       }
       session.setAttribute("filelib.CURENTRY", entry);
       sendFile(fileEditJSP,req,resp);
       return;
     }

     str = req.getParameter("SubmitAdd");
     if ( str != null )
     {
       String tmp = (String) session.getAttribute("filelib.CURFOLDER");
       int parentID = home.ROOT;
       if (tmp != null && tmp.length() > 0)
         parentID = Integer.parseInt(tmp);
       FileLibEntry parent = home.get(parentID);
       if (parent == null)
       {
         sendFile(BoxStatic.errorJSP,req,resp);
         return;
       }

       if (!home.checkAccessRights(aBoxSession.getID(), parent, home.WRITE_ALLOW))
       {
         logger.warn("Access rights violation for user " + aBoxSession.getLoginID() + " " + parent.getName());
         sendFile(BoxStatic.accessRightErrorJSP,req,resp);
         return;
       }
       entry = new FileLibEntry();
       entry.setParent(parentID);
       entry.setId(-1);
       entry.setIsfolder(false);
       entry.setOwnerid(aBoxSession.getID());
       entry.setCreatetime(new Timestamp(System.currentTimeMillis()));
       entry.setLastmodifiedtime(new Timestamp(System.currentTimeMillis()));
       entry.setAttributes(home.INHERIT);

       session.setAttribute("filelib.CURENTRY", entry); //new folder
       sendFile(fileEditJSP,req,resp);
       return;
     }

     str = req.getParameter("SubmitDelete");
     if ( str != null )
     {
       String tmp = (String) req.getParameter("ID");
       int ID = -1;
       if (tmp != null && tmp.length() > 0)
         ID = Integer.parseInt(tmp);

       if (ID > 0 )
         entry = home.get(ID);
       if (entry == null || entry.getIsfolder())
       {
         String msg = "Error geting file:" + tmp;
         logger.error(msg);
         sendError(msg,req,resp);
         return;
       }

       if (!home.checkAccessRights(aBoxSession.getID(), entry, home.WRITE_ALLOW))
       {
         logger.warn("Access rights violation for user " + aBoxSession.getLoginID() + " " + entry.getName());
         sendFile(BoxStatic.accessRightErrorJSP,req,resp);
         return;
       }
       home.deleteFile(aBoxSession.getID(), entry);
       entry = home.get(entry.getParent());
       session.setAttribute("filelib.CURENTRY", entry);
       sendFile(folderContentJSP,req,resp);
     }
     sendError("Nothing here",req,resp);
   }

   public void setFileViewJSP(String str)
   {
     fileViewJSP = str;
   }
   public void setFileEditJSP(String str)
   {
     fileEditJSP = str;
   }
   public void setFolderContentJSP(String str)
   {
     folderContentJSP = str;
   }
}
